#!/bin/bash

echo "=== 系统串口检测工具 ==="
echo ""

# 定义常见的串口设备模式
SERIAL_PATTERNS=(
    "ttyS"      # 标准串口
    "ttyUSB"    # USB串口
    "ttyAMA"    # ARM串口
    "ttyACM"    # USB ACM设备
    "ttyXR"     # 扩展串口
    "ttyMX"     # 多路复用串口
    "ttywsSES"  # 您提到的类型
    "ttyXRUSB"  # USB扩展串口
    "ttyXRACM"  # USB ACM扩展
)

echo "1. 系统所有串口设备文件:"
echo "=================="
for pattern in "${SERIAL_PATTERNS[@]}"; do
    devices=$(ls /dev/${pattern}* 2>/dev/null)
    if [ -n "$devices" ]; then
        echo "模式 ${pattern}:"
        echo "$devices" | head -5
        echo ""
    fi
done
echo "=================="
echo ""

# 检查dmesg中的串口信息
echo "2. 内核日志中的串口信息:"
echo "=================="
dmesg | grep -i -E "(ttyS|ttyUSB|ttyAMA|ttyACM|serial|uart)" | head -15
echo "=================="
echo ""

# 测试常见串口的可用性
echo "3. 串口可用性测试:"
echo "=================="
echo "标准串口 (ttyS):"
for i in {0..7}; do
    if stty -F /dev/ttyS$i 2>/dev/null; then
        echo "  ttyS$i: 可用"
    else
        echo "  ttyS$i: 不可用"
    fi
done

echo ""
echo "USB串口 (ttyUSB):"
for i in {0..3}; do
    if [ -e "/dev/ttyUSB$i" ]; then
        if stty -F /dev/ttyUSB$i 2>/dev/null; then
            echo "  ttyUSB$i: 可用"
        else
            echo "  ttyUSB$i: 存在但不可用"
        fi
    fi
done

echo ""
echo "ARM串口 (ttyAMA):"
for i in {0..3}; do
    if [ -e "/dev/ttyAMA$i" ]; then
        if stty -F /dev/ttyAMA$i 2>/dev/null; then
            echo "  ttyAMA$i: 可用"
        else
            echo "  ttyAMA$i: 存在但不可用"
        fi
    fi
done
echo "=================="
echo ""

# 检查setserial（如果可用）
if command -v setserial >/dev/null 2>&1; then
    echo "4. setserial 信息:"
    echo "=================="
    setserial -g /dev/ttyS* 2>/dev/null | head -10
    echo "=================="
    echo ""
fi

# 检查udev规则
echo "5. udev串口规则:"
echo "=================="
if [ -d "/etc/udev/rules.d" ]; then
    grep -r "tty" /etc/udev/rules.d/ 2>/dev/null | grep -E "(SERIAL|USB|ACM)" | head -5
else
    echo "未找到udev规则目录"
fi
echo "=================="
echo ""

# 检查当前虚拟串口
echo "6. 当前虚拟串口设备:"
echo "=================="
ls -la /dev/ttyV* 2>/dev/null || echo "没有找到虚拟串口设备"
echo "=================="
echo ""

# 统计信息
echo "7. 串口设备统计:"
echo "=================="
total_devices=0
for pattern in "${SERIAL_PATTERNS[@]}"; do
    count=$(ls /dev/${pattern}* 2>/dev/null | wc -l)
    if [ $count -gt 0 ]; then
        echo "${pattern}: $count 个设备"
        total_devices=$((total_devices + count))
    fi
done
echo "总计: $total_devices 个串口设备"
echo "=================="
echo ""

# 提供建议
echo "8. 建议的虚拟串口命名:"
echo "=================="
echo "推荐使用以下命名避免冲突:"
echo "  ttyV0, ttyV1, ttyV2, ... (推荐，避免所有冲突)"
echo "  virtual_serial1, virtual_serial2, ... (描述性名称)"
echo "  my_serial1, my_serial2, ... (自定义前缀)"
echo ""
echo "避免使用的命名:"
echo "  ttyS* (可能被系统占用)"
echo "  ttyUSB* (可能被USB设备占用)"
echo "  ttyAMA* (可能被ARM设备占用)"
echo "=================="
echo ""

# 检查配置文件
echo "9. 配置文件检查:"
echo "=================="
if [ -f "vserial.conf" ]; then
    echo "找到配置文件: vserial.conf"
    echo "当前配置:"
    grep -v "^#" vserial.conf | grep -v "^$" | head -5
else
    echo "未找到配置文件 vserial.conf"
    echo "建议创建配置文件，使用 ttyV* 命名"
fi
echo "==================" 